package net.oschina.app.ui.activity.me;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.Button;

import net.oschina.app.R;
import net.oschina.app.base.BaseActivity;
import net.oschina.app.util.ImageUtils;
import net.oschina.app.util.ToolBarUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

public class MyInfoActivity extends BaseActivity {
    public static final int Cut_PHOTO = 1;
    public static final int USER_HEAD = 2017;
    public static final int SHOW_PHOTO = 2;
    public static final int PHOTO_ALBUM = 3;
    public static final int SHOW_PHOTO_ALBUM = 4;
    public static final int TAKEING_PHOTO = 9527;
    private Uri imageUri;
    private File mOutputImage;
    private File mOutputImage1;
    private HeadPictureDialog mPictureDialog2;
    @BindView(R.id.iv_my_info)
    CircleImageView mIvMyInfo;
    @BindView(R.id.btn_back)
    Button mBtnBack;

    @Override
    public int getLayoutRes() {
        return R.layout.activity_me_myinfo;
    }

    @Override
    public void initView() {
        ToolBarUtil.setToolBar(this, "我的资料");
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        /*  //设置标题，返回键可见
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("我的资料");*/
        //判断头像是否存在本地
        Bitmap bitmap = ImageUtils.getBitmap(getApplicationContext(), "head.jpg");
        if (bitmap != null) {
            mIvMyInfo.setImageBitmap(bitmap);
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.iv_my_info)
    public void onClick() {
        final HeadPictureDialog pictureDialog = new HeadPictureDialog(this);
        pictureDialog.setTitle("选择操作");
        pictureDialog.setChangeHeadOnClickListener("更换头像", new HeadPictureDialog.onChangeHeadOnClickListener() {
            @Override
            public void onChangeClick() {
                Changpicture();
                pictureDialog.dismiss();
            }
        });
       /* pictureDialog.setCheckHeadOnClickListener("查看头像", new HeadPictureDialog.onCheckHeadOnClickListener() {
            @Override
            public void onCheckClick() {
                Intent intent = new Intent(this, DialogActivity.class);
                intent.putExtra("IMAGE",imageUri);
                startActivity(intent);
                pictureDialog.dismiss();

            }
        });*/
        pictureDialog.setNoOnclickListener("取消", new HeadPictureDialog.onNoOnclickListener() {
            @Override
            public void onNoClick() {
                finish();
            }
        });
        pictureDialog.show();

    }

    private void Changpicture() {

        mPictureDialog2 = new HeadPictureDialog(this);
        mPictureDialog2.setTitle("选择图片");
        mPictureDialog2.setChangeHeadOnClickListener("相册", new HeadPictureDialog.onChangeHeadOnClickListener() {
            @Override
            public void onChangeClick() {
                openSystemAlbum();
                mPictureDialog2.dismiss();
            }
        });
        mPictureDialog2.setCheckHeadOnClickListener("相机", new HeadPictureDialog.onCheckHeadOnClickListener() {
            @Override
            public void onCheckClick() {
                openSystemCamera();

                mPictureDialog2.dismiss();

            }
        });
        mPictureDialog2.setNoOnclickListener("取消", new HeadPictureDialog.onNoOnclickListener() {
            @Override
            public void onNoClick() {
                mPictureDialog2.dismiss();
            }
        });
        mPictureDialog2.show();


    }

    public void openSystemCamera() {
        //创建File对象,用于存储选择的照片
        mOutputImage1 = new File(Environment.getExternalStorageDirectory(), "head.jpg");
        try {
            if (mOutputImage1.exists()) {
                mOutputImage1.delete();  //如果有就删除之前的头像
            }
            //创建新的File
            mOutputImage1.createNewFile();

        } catch (IOException e) {
            e.printStackTrace(); //创建新的File
        }
        //获取存储头像图片的路径
        imageUri = Uri.fromFile(mOutputImage1);
        //隐式意图启动相机
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        // 启动相机程序
        startActivityForResult(intent, Cut_PHOTO);
    }
    //打开系统相册功能
    public void openSystemAlbum() {
        mOutputImage = new File(MyInfoActivity.this.getCacheDir(), "head.jpg");
        try {
            if (mOutputImage.exists()) {
                mOutputImage.delete();
            }
            mOutputImage.createNewFile();

        } catch (IOException e) {
            e.printStackTrace();
        }
        imageUri = Uri.fromFile(mOutputImage);
        Intent intent = new Intent("android.intent.action.GET_CONTENT");
        intent.setType("image/*");
        //允许裁剪
        intent.putExtra("crop", true);
        //允许缩放
        intent.putExtra("scale", true);
        //图片的输出位置
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        startActivityForResult(intent,PHOTO_ALBUM);
        // 创建File对象,用于存储选择的照片
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case Cut_PHOTO:
                if (resultCode == RESULT_OK) {
                    Intent intent = new Intent("com.android.camera.action.CROP");
                    intent.setDataAndType(imageUri, "image/*");
                    intent.putExtra("scale", true);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                    // 启动裁剪
                    startActivityForResult(intent, SHOW_PHOTO);

                }
                break;
            case SHOW_PHOTO:
                if (resultCode == RESULT_OK) {
                    try {
                        Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
                        // 将裁剪后的照片显示出来
                        mIvMyInfo.setImageBitmap(bitmap);
                        //Dialog消失
                        mPictureDialog2.dismiss();
                        try {
                            ImageUtils.saveImage(getApplicationContext(),"head.jpg",bitmap);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        Intent userData = new Intent();
                        this.setResult(USER_HEAD, userData);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }
                break;
            case PHOTO_ALBUM:
                if (resultCode == RESULT_OK) {
                    imageUri = data.getData();
                    Intent intent = new Intent("com.android.camera.action.CROP");
                    intent.setDataAndType(imageUri, "image/*");
                    intent.putExtra("scale", true);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                    // 启动裁剪
                    startActivityForResult(intent, SHOW_PHOTO_ALBUM);

                }
                break;
            case SHOW_PHOTO_ALBUM:
                if (resultCode == RESULT_OK) {
                    try {
                        Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
                        // 将裁剪后的照片显示出来
                        mIvMyInfo.setImageBitmap(bitmap);
                        mPictureDialog2.dismiss();
                        try {
                            ImageUtils.saveImage(getApplicationContext(),"head.jpg",bitmap);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        Intent userData = new Intent();
                        this.setResult(USER_HEAD, userData);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }    break;

            default:
                break;
        }

    }
}
